package com.springdemo.aop;


import com.springdemo.annotation.TransParm;
import org.aopalliance.intercept.Joinpoint;
import org.aspectj.lang.JoinPoint;
import org.aspectj.lang.annotation.AfterReturning;
import org.aspectj.lang.annotation.Aspect;
import org.aspectj.lang.annotation.Pointcut;
import org.springframework.context.annotation.Configuration;

import java.lang.reflect.Field;

@Aspect
@Configuration
public class TestAop {

    @Pointcut("@annotation(com.springdemo.annotation.TestMethod)")
    public void cut() {

    }

    @AfterReturning(pointcut = "cut()", returning = "returnValue")
    public void doAfter(JoinPoint point, Object returnValue) throws IllegalAccessException {
       point.getSignature();
       Class clazz = returnValue.getClass();
       Field[] fields = clazz.getDeclaredFields();
        for (Field field : fields) {
            if (field.isAnnotationPresent(TransParm.class)){

                TransParm transParm = field.getAnnotation(TransParm.class);
                String code = transParm.value();
                field.setAccessible(true);
                field.set(returnValue,"新的值"+code);
            }
        }
    }

}
